package com.csyf.manager.controller.system;

import com.csyf.manager.common.CommonConst;
import com.csyf.manager.entity.SysPayProduct;
import com.csyf.manager.service.SysProductService;
import com.csyf.manager.utils.ResultEntity;
import com.csyf.manager.utils.ResultUtli;
import com.csyf.manager.utils.page.JqGridVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: casper
 * @Description: 产品管理
 * @Date: Created in 2018/12/11 15:49
 */
@Controller
@RequestMapping("/product")
public class SysProductController {

    @Autowired
    private SysProductService sysProductService;

    /**
     * 产品列表
     * @param vo
     * @param page
     * @param rows
     * @return
     */
    @PostMapping(value = "list")
    @ResponseBody
    public JqGridVo list(SysPayProduct vo, Integer page, Integer rows) {
        return sysProductService.queryProudctList(vo, page, rows);
    }

    /**
     * 新增产品
     * @param productForm
     * @return
     */
    @ResponseBody
    @PostMapping(value = "add")
    public ResultEntity add(SysPayProduct productForm) {
        try {
            sysProductService.saveProduct(productForm);
            return ResultUtli.DEFAULT_SUCCESS("添加产品成功", null);
        } catch (Exception e) {
            return ResultUtli.DEFAULT_ERROR("添加产品失败。"+ e.getMessage());
        }
    }

    /**
     * 编辑产品详情
     * @param model
     * @param productId
     * @return
     */
    @GetMapping("edit")
    public String editProduct(Model model, Long productId) {
        SysPayProduct product = sysProductService.queryProductById(productId);
        model.addAttribute("product", product);
        return "product/productDetail";
    }

    /**
     * 编辑产品
     * @param productForm
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public ResultEntity editAgent(SysPayProduct productForm) {
        int num = sysProductService.updateByProductId(productForm);
        if (num > 0) {
            return ResultUtli.DEFAULT_SUCCESS("编辑产品成功", null);
        } else {
            return ResultUtli.DEFAULT_ERROR("编辑产品失败");
        }
    }

    /**
     * 删除产品
     * @param productId
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public ResultEntity deleteProduct(String productId) {
        List<String> productIdsListy = Arrays.asList(productId);
        int num = sysProductService.updateStatusBatch(productIdsListy, CommonConst.STATUS_INVALID);
        if (num > 0) {
            return ResultUtli.DEFAULT_SUCCESS("删除产品成功", null);
        } else {
            return ResultUtli.DEFAULT_ERROR("删除产品失败");
        }
    }

    /**
     * 批量删除产品
     * @param productIdStr
     * @return
     */
    @GetMapping("/batch/delete")
    @ResponseBody
    public ResultEntity batchDeleteProduct(String productIdStr) {
        String[] productIds = productIdStr.split(",");
        List<String> productIdsList = Arrays.asList(productIds);
        int num = sysProductService.updateStatusBatch(productIdsList, CommonConst.STATUS_INVALID);
        if (num > 0) {
            return ResultUtli.DEFAULT_SUCCESS("删除产品成功", null);
        } else {
            return ResultUtli.DEFAULT_ERROR("删除产品失败");
        }

    }


}
